package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class studentapply
 */
@WebServlet("/src/studentapply")
public class studentapply extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public studentapply() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String studentid = request.getParameter("student_id");
	    String courseid = request.getParameter("courseid");
	    String reason = request.getParameter("reason");

	    hello a = new hello();//模板
	    try {
	    	ResultSet b;
	    	b=a.doselect("SELECT course_teacher,course_name from course where course_id= ? ", courseid);
	    	if(b.next()) {
	    		String teacherid=b.getString("course_teacher");
	    		String coursename=b.getString("course_name");
	    		ResultSet c;
	    		c=a.doselect("SELECT student_name from student where student_id= ? ", studentid);
	    		if(c.next()) {
	    			String studentname=c.getString("student_name");
	    	    String content="学生姓名: "+studentname+" 学生id: "+studentid+" 申请 "+" 课程名称: "+coursename+" 课程id: "+courseid+" 的缓考 "+" 理由: "+reason;

	    		a.doupdate("INSERT INTO message ( message_object, message_objectid, message_content, message_sender, message_senderid ) VALUES ('teacher', ? , ? ,'student', ? )",teacherid,content, studentid);
	    		response.getWriter().write("true");
	    		}
	    		c.next();
	    	}
	    	b.close();

	    }catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板
	    }

}
